﻿using System;
using System.Collections;
using System.Collections.Generic;
//using IBatisNet.DataAccess;
using AUV5.Model;
using System.Web;
using AUV5.Common;
using System.IO;

namespace AUV5.BLL
{
    /// <summary>
    /// 功能：     WorksShare业务逻辑实现
    /// 作者：     LI
    /// 日期：     1/13/2012
    /// </summary>
    public class WorksShareBLL
    {
        private AUV5.DAL.Interface.IWorksShareDao dao = null;

        private WorksShareBLL()
        {
            dao = AUV5.DAL.Dao.WorksShareSqlMapDao.Instance;
        }

        #region （懒汉）单例模式
        private static WorksShareBLL instance = null;
        private static object syncRoot = new object();
        public static WorksShareBLL Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (syncRoot)
                    {
                        if (instance == null)
                        {
                            instance = new WorksShareBLL();
                        }
                    }
                }
                return instance;
            }
        }
        #endregion


        #region	基本功能 不能修改


        /// <summary>
        /// 根据主键得到明细
        /// </summary>
        /// <returns></returns>
        public WorksShare GetDetail(System.String shareid)
        {
            try
            {
                return dao.GetDetail(shareid);
            }
            catch
            {
                throw;
            }
        }


        /// <summary>
        /// 得到列表
        /// </summary>
        /// <returns></returns>
        public IList<WorksShare> GetList()
        {
            try
            {
                IList<WorksShare> list = dao.GetList();
                return list;
            }
            catch
            {
                throw;
            }
        }

        /// <summary>
        /// 通过不同参数得到列表
        /// </summary>
        /// <param name="TopCounts"></param>
        /// <returns></returns>
        private IList<WorksShare> GetListByParams(object TopCounts, WorksShare worksshare)
        {
            try
            {
                Hashtable ht = new Hashtable();
                ht.Add("TopCounts", TopCounts);
                IList<WorksShare> list = dao.GetListByParams(ht);
                return list;
            }
            catch
            {
                throw;
            }
        }



        /// <summary>
        /// 新建
        /// </summary>
        /// <param name="worksshare"></param>
        /// <returns></returns>
        public object Add(WorksShare worksshare)
        {
            try
            {
                return dao.Add(worksshare);
            }
            catch
            {
                throw;
            }
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="worksshare"></param>
        /// <returns></returns>
        public int Update(WorksShare worksshare)
        {
            try
            {
                return dao.Update(worksshare);
            }
            catch
            {
                throw;
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="ShareID"></param>
        /// <returns></returns>
        public int Delete(System.String shareid)
        {
            try
            {
                return dao.Delete(shareid);
            }
            catch
            {
                throw;
            }
        }

        #endregion


        #region 扩展功能

        #region 添加成果分享

        /// <summary>
        /// 添加成果分享
        /// </summary>
        /// <param name="userID"></param>
        /// <param name="orderID"></param>
        /// <param name="format"></param>
        /// <param name="attachPath"></param>
        public void AddWorkShare(string userID, string serviceID, string description, Common.Enum.AttachFormat format, string attachPath)
        {
            try
            {
                ServiceInfo service = BLL.ServiceInfoBLL.Instance.GetDetail(serviceID);
                if (service != null)
                {
                    string path = "";
                    switch (format)
                    {
                        case AUV5.Common.Enum.AttachFormat.Video:
                            Common.FormatConverter converter = new Common.FormatConverter();

                            string sourceFile = HttpContext.Current.Server.MapPath(attachPath);
                            string destpath = "/UploadFile/Share/Video/" + DateTime.Now.Year.ToString() + "/" + DateTime.Now.Month.ToString() + "/" + DateTime.Now.Day.ToString() + "/";
                            if (!System.IO.Directory.Exists(HttpContext.Current.Server.MapPath(destpath)))
                            {
                                System.IO.Directory.CreateDirectory(HttpContext.Current.Server.MapPath(destpath));
                            }
                            string uniquename = FileHelper.GetUniqueFileName();

                            //转换MP4格式
                            FormatConverter mp4converter = new FormatConverter();
                            string mp4 = mp4converter.Convert(sourceFile, destpath, uniquename, FormatConverter.VideoType.MP4, false, false);

                            //转换Flv格式
                            FormatConverter flvconverter = new FormatConverter();
                            string flv = flvconverter.Convert(sourceFile, destpath, uniquename, FormatConverter.VideoType.FLV, true, true);

                            path = flvconverter.DestVideo;


                            //在转换完成之前无法对图片进行处理
                            //string coverpath = GetCoverImagePath(flvconverter.DestVideo);

                            //ImageProcess.ImageToProperSize(flvconverter.DestImage, CommonDefine.SHARE_IMAGE_WIDTH, CommonDefine.SHARE_IMAGE_HEIGHT, coverpath, null);

                            break;
                        case AUV5.Common.Enum.AttachFormat.Image:
                            path = "/UploadFile/Share/Image/" + DateTime.Now.Year.ToString() + "/" + DateTime.Now.Month.ToString() + "/" + DateTime.Now.Day.ToString() + "/" + FileHelper.GetUniqueFileName() + ".jpg";
                            string coverpath = GetImageCover(path);
                            string previewpath = GetImagePreview(path);
                            ImageProcess.ImageToProperWidth(attachPath, CommonDefine.SHARE_IMAGE_WIDTH, path);
                            ImageProcess.ImageToProperSize(attachPath, CommonDefine.SHARE_IMAGE_WIDTH, CommonDefine.SHARE_IMAGE_HEIGHT, coverpath, null);
                            FileHelper.Copy(attachPath,previewpath);
                            break;
                        default:
                            break;
                    }

                    WorksShare share = new WorksShare();
                    share.ShareID = Guid.NewGuid().ToString();
                    share.UserID = userID;
                    share.ServiceID = serviceID;
                    share.AttachFormat = (int)format;
                    share.AttachPath = path;

                    share.StatusID = (int)Common.Enum.WorkShareStatus.Active;
                    share.Description = description;

                    share.State = (int)Common.Enum.auvState.Enabled;
                    share.IsDel = (int)Common.Enum.auvDel.NotDel;
                    share.CreateDate = DateTime.Now;
                    share.UpdateDate = DateTime.Now;
                    share.BeginDate = DateTime.Now;
                    share.EndDate = DateTime.Now;

                    Add(share);
                }
            }
            catch (Exception)
            {

                throw;
            }
        }

        public string GetImagePreview(string path)
        {
            return path.Replace(Path.GetExtension(path), "_preview.jpg");
        }

        public string GetImageCover(string path)
        {
            return path.Replace(Path.GetExtension(path), "_cover.jpg");
        }

        public string GetVideoCover(string path)
        {
            return Path.ChangeExtension(path, ".jpg");
        }
        #endregion

        #region 获取分页数据
        /// <summary>
        /// 获取分页的分享
        /// </summary>
        /// <param name="pagesize"></param>
        /// <param name="pageindex"></param>
        /// <returns></returns>
        public IList<WorksShare> GetPagedShare(int pagesize, int pageindex,ref int recordcount)
        {
            try
            {
                NetPager pager = new NetPager();
                pager.TableName = "WorksShare";
                pager.PrimaryKey = "ShareID";
                pager.Order = "createDate desc,ShareID desc";
                pager.SortType = (int)Common.Enum.SortType.Multi;
                pager.Where = "";
                pager.PageSize = pagesize;
                pager.PageIndex = pageindex;
                IList<WorksShare> list = dao.GetPagedShare(pager);
                recordcount = pager.TotalCount;
                return list;
            }
            catch (Exception)
            {
                throw;
            }
        }
        #endregion

        #endregion
    }
}
